package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.ContainerEntity;
import io.renren.modules.sys.entity.OrdertrackingEntity;
import io.renren.modules.sys.service.ContainerService;
import io.renren.modules.sys.service.OrdertrackingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订单跟踪表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-11-26 23:59:51
 */
@RestController
@RequestMapping("sys/finance")
public class FinanceController {
	@Autowired
	private OrdertrackingService	ordertrackingService;
	@Autowired
	private ContainerService		containerService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:finance:list")
	public R list(@RequestParam Map<String, Object> params) {

		PageUtils page = ordertrackingService.queryPage(params);

		return R.ok().put("page", page);
	}

	// https://blog.csdn.net/weixin_30512027/article/details/83270344
	/**
	 * 信息
	 */
	@RequestMapping("/info/{number}")
	@RequiresPermissions("sys:finance:info")
	public R info(@PathVariable("number") Integer number) {
		OrdertrackingEntity finance = ordertrackingService.selectById(number);
		Wrapper<ContainerEntity> wrapper = new EntityWrapper<ContainerEntity>();
		// QueryWrapper<ContainerEntity> queryWrapper = new QueryWrapper<>();
		// wrapper = new Condition();
		wrapper.eq("order_id", number);
		List<ContainerEntity> containerList = containerService.selectList(wrapper);
		return R.ok().put("finance", finance).put("containerList", containerList);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:finance:save")
	public R save(@RequestBody OrdertrackingEntity finance) {
		ordertrackingService.insert(finance);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:finance:update")
	public R update(@RequestBody OrdertrackingEntity finance) {
		ordertrackingService.updateById(finance);

		return R.ok();
	}

	/**
	 * 通过审核和取消审核
	 * @param number
	 * @return
	 */
	@RequestMapping("/toggleChecked/{number}")
	@RequiresPermissions("sys:finance:update")
	public R toggleChecked(@PathVariable("number") Integer number) {
		OrdertrackingEntity entity = ordertrackingService.selectById(number);
		if (entity != null) {
			Boolean checked = entity.getChecked();
			if (checked == null) {
				checked = false;
			}
			entity.setChecked(!checked);
			ordertrackingService.updateById(entity);
			return R.ok();
		} else {
			return R.error("客户信息不存在");
		}

	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:finance:delete")
	public R delete(@RequestBody Integer[] numbers) {
		ordertrackingService.deleteBatchIds(Arrays.asList(numbers));

		return R.ok();
	}

}
